$(document).ready(function(){
	searchArticleClassification();
});

function searchArticleClassification(){
	$('#table_list').DataTable({
        dom: '<"top">rt<"bottom"flip><"clear">',
        language: {
            "sUrl":'../static/plugins/datatables/language.json'
        },
        "searching": false,
        "processing": true,
        "serverSide": true,
        ajax:{
            url:'/article-classification/get-article-classification',
            type:'post',
            data:function(d){
            }
        },
        aoColumns: [
            { "data": "", "bSortable": false},
            { "data": "id", "bSortable": true},
            { "data": "name", "bSortable": false },
            { "data": "sort", "bSortable": true },
            { "data": "use_flg", "bSortable":false }
        ],
        columnDefs: [
            {
                "targets": [0],
                "orderable":false,
                "searchable":false,
                "render": function(data, type, full) {
                    return '<a href="javascript:" onclick="changeArticleClassification('+ full.id +',\''+full.name+'\',\''+full.sort+'\',\''+full.use_flg+'\')">修改</a>';
                }
            },
            {
                "targets": [4],
                "data": "use_flg",
                "render": function(data, type, full) {
                	return '<span class="switch" data-id="'+ full.id +'" data-flg="'+(data == 1 ? 0 : 1)+'"><input  style="width:20px;height:20px;" type="checkbox" '+(data == 1 ? 'checked="checked"' : '')+' class="input-check"></span>';
                }
            }
        ]
    });
}

function changeArticleClassification(id,name,sort,use_flg){
	$('#article-classification-modal').modal('show');
	$("#article_classification_id").val(id);
	$('#name').val(name);
	$('#sort').val(sort);
	$('#use_flg').val(use_flg);
}

function changeArticleClassificationStatus(id,use_flg){
	title = '确定禁用此商品标识吗？';
	if(use_flg == 1){
		title = '确定启用此商品标识吗？';
	}
	$.jBox.confirm(title, "提示", function(v){
        if (v == 'ok'){
            $.post('/article-classification/change-article-classification-status',{id:id,use_flg:use_flg},success,'json');
            return false;
            function success(data){
                $.jBox.close();
                if(data.code == 200)
                {
                    $.jBox.tip('处理成功', 'success');
                    window.setTimeout(function () { table_list_reload(); }, 1000);
                }
                else{
                    $.jBox.error(data.val, '错误');
                }
            }
        }else{
        	if(use_flg == 1){
        		$('#table_list').find('span[data-id='+id+']').children('input').prop('checked',false);
        	}else{
        		$('#table_list').find('span[data-id='+id+']').children('input').prop('checked',true);
        	}
            $.jBox.close();
        }
    });
}

function table_list_reload(){
    var table = $('#table_list').DataTable();
    table.ajax.reload();
}


$('.add-article-classification').click(function(){
	$('#article-classification-modal').modal('show');
	$("#article_classification_id").val('');
	$('#name').val('');
	$('#sort').val('');
	$('#use_flg').val(1);
});

$('.ajax_post_tag').click(function(){
	var form = $('#article_classification_form');
    var name = $('#name').val();
    if(name == ''){
        $.jBox.error('请输入商品标识名称', '错误');
        return false;
    }
    var sort = $('#sort').val();
    if(sort == ''){
        $.jBox.error('请输入热销组排序编号', '错误');
        return false;
    }
    $.post(form.attr('action'),form.serialize(),success,'json');
    return false;
	function success(data){
        if(data.code == 200)
        {
            $.jBox.tip('处理成功', 'success');
            window.setTimeout(function () {
                $('#article-classification-modal').modal('hide');
                table_list_reload();
            }, 1000);
        }
        else{
            $.jBox.error(data.val, '错误');
        }
	}
});

$('table').on('click','.input-check',function(){
	var id = $(this).parent().attr('data-id');
	var use_flg = $(this).parent().attr('data-flg');
	changeArticleClassificationStatus(id,use_flg);
});